{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "start_time": "2023-05-11T10:37:38.884050Z",
     "end_time": "2023-05-11T10:37:38.886044Z"
    }
   },
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (558704673.py, line 6)",
     "output_type": "error",
     "traceback": [
      "\u001B[0;36m  Cell \u001B[0;32mIn[23], line 6\u001B[0;36m\u001B[0m\n\u001B[0;31m    imort pandas as pd\u001B[0m\n\u001B[0m          ^\u001B[0m\n\u001B[0;31mSyntaxError\u001B[0m\u001B[0;31m:\u001B[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import mne\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "imort pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "from MyEDFImports import import_ecg, get_edf_filenames, import_eeg"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [],
   "source": [
    "edfs_names = get_edf_filenames()\n",
    "edfs_names\n",
    "tmin = 100\n",
    "tmax = 101"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-05-11T10:24:02.146632Z",
     "end_time": "2023-05-11T10:24:02.151662Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting EDF parameters from /mnt/c/Tadeusz/mgr_sleep_states/Jean-Pol_repaired_headers/CN223100.edf...\n",
      "EDF file detected\n",
      "Setting channel info structure...\n",
      "Creating raw.info structure...\n",
      "NOTE: pick_channels() is a legacy function. New code should use inst.pick(...).\n",
      "Extracting EDF parameters from /mnt/c/Tadeusz/mgr_sleep_states/Jean-Pol_repaired_headers/CP229110.edf...\n",
      "EDF file detected\n",
      "Setting channel info structure...\n",
      "Creating raw.info structure...\n",
      "NOTE: pick_channels() is a legacy function. New code should use inst.pick(...).\n",
      "Extracting EDF parameters from /mnt/c/Tadeusz/mgr_sleep_states/Jean-Pol_repaired_headers/CP229110_wake.edf...\n",
      "EDF file detected\n",
      "Setting channel info structure...\n",
      "Creating raw.info structure...\n",
      "NOTE: pick_channels() is a legacy function. New code should use inst.pick(...).\n",
      "Extracting EDF parameters from /mnt/c/Tadeusz/mgr_sleep_states/Jean-Pol_repaired_headers/CX230050.edf...\n",
      "EDF file detected\n",
      "Setting channel info structure...\n",
      "Creating raw.info structure...\n",
      "NOTE: pick_channels() is a legacy function. New code should use inst.pick(...).\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/mnt/c/Tadeusz/mgr_sleep_states/MyEDFImports.py:9: RuntimeWarning: Channel names are not unique, found duplicates for: {'CHIN EMG'}. Applying running numbers for duplicates.\n",
      "  raw = mne.io.read_raw_edf(path + \"//\" + name)\n",
      "/mnt/c/Tadeusz/mgr_sleep_states/MyEDFImports.py:9: RuntimeWarning: Channel names are not unique, found duplicates for: {'CHIN EMG'}. Applying running numbers for duplicates.\n",
      "  raw = mne.io.read_raw_edf(path + \"//\" + name)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting EDF parameters from /mnt/c/Tadeusz/mgr_sleep_states/Jean-Pol_repaired_headers/DG220020.edf...\n",
      "EDF file detected\n",
      "Setting channel info structure...\n",
      "Creating raw.info structure...\n",
      "NOTE: pick_channels() is a legacy function. New code should use inst.pick(...).\n",
      "Extracting EDF parameters from /mnt/c/Tadeusz/mgr_sleep_states/Jean-Pol_repaired_headers/DO223050.edf...\n",
      "EDF file detected\n",
      "Setting channel info structure...\n",
      "Creating raw.info structure...\n",
      "NOTE: pick_channels() is a legacy function. New code should use inst.pick(...).\n",
      "Extracting EDF parameters from /mnt/c/Tadeusz/mgr_sleep_states/Jean-Pol_repaired_headers/LA216100.edf...\n",
      "EDF file detected\n",
      "Setting channel info structure...\n",
      "Creating raw.info structure...\n",
      "NOTE: pick_channels() is a legacy function. New code should use inst.pick(...).\n",
      "Extracting EDF parameters from /mnt/c/Tadeusz/mgr_sleep_states/Jean-Pol_repaired_headers/LM230010.edf...\n",
      "EDF file detected\n",
      "Setting channel info structure...\n",
      "Creating raw.info structure...\n",
      "NOTE: pick_channels() is a legacy function. New code should use inst.pick(...).\n",
      "Extracting EDF parameters from /mnt/c/Tadeusz/mgr_sleep_states/Jean-Pol_repaired_headers/TK221110.edf...\n",
      "EDF file detected\n",
      "Setting channel info structure...\n",
      "Creating raw.info structure...\n",
      "NOTE: pick_channels() is a legacy function. New code should use inst.pick(...).\n",
      "Extracting EDF parameters from /mnt/c/Tadeusz/mgr_sleep_states/Jean-Pol_repaired_headers/VC209100.edf...\n",
      "EDF file detected\n",
      "Setting channel info structure...\n",
      "Creating raw.info structure...\n",
      "NOTE: pick_channels() is a legacy function. New code should use inst.pick(...).\n",
      "Extracting EDF parameters from /mnt/c/Tadeusz/mgr_sleep_states/Jean-Pol_repaired_headers/VP214110.edf...\n",
      "EDF file detected\n",
      "Setting channel info structure...\n",
      "Creating raw.info structure...\n",
      "NOTE: pick_channels() is a legacy function. New code should use inst.pick(...).\n",
      "Extracting EDF parameters from /mnt/c/Tadeusz/mgr_sleep_states/Jean-Pol_repaired_headers/WD224010.edf...\n",
      "EDF file detected\n",
      "Setting channel info structure...\n",
      "Creating raw.info structure...\n",
      "NOTE: pick_channels() is a legacy function. New code should use inst.pick(...).\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/mnt/c/Tadeusz/mgr_sleep_states/MyEDFImports.py:9: RuntimeWarning: Channel names are not unique, found duplicates for: {'CHIN EMG'}. Applying running numbers for duplicates.\n",
      "  raw = mne.io.read_raw_edf(path + \"//\" + name)\n"
     ]
    }
   ],
   "source": [
    "edfs = []\n",
    "for edf in edfs_names:\n",
    "    new = import_ecg(edf, tmin , tmax)\n",
    "    edfs.append(new)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-05-11T10:24:03.960184Z",
     "end_time": "2023-05-11T10:24:05.184962Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting EDF parameters from /mnt/c/Tadeusz/mgr_sleep_states/Jean-Pol_repaired_headers/CP229110.edf...\n",
      "EDF file detected\n",
      "Setting channel info structure...\n",
      "Creating raw.info structure...\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_2308/3776302725.py:3: RuntimeWarning: Channel names are not unique, found duplicates for: {'CHIN EMG'}. Applying running numbers for duplicates.\n",
      "  raw = mne.io.read_raw_edf(edf_dir + '//' + name)\n"
     ]
    },
    {
     "data": {
      "text/plain": "<Info | 7 non-empty values\n bads: []\n ch_names: mastoid 1-2, FP1A2 EEG, FP2A1 EEG, F7A2 EEG, F3A2 EEG, FZA2 EEG, ...\n chs: 35 EEG\n custom_ref_applied: False\n highpass: 0.0 Hz\n lowpass: 250.0 Hz\n meas_date: 2005-11-29 21:35:12 UTC\n nchan: 35\n projs: []\n sfreq: 500.0 Hz\n>",
      "text/html": "<table class=\"table table-hover table-striped table-sm table-responsive small\">\n    <tr>\n        <th>Measurement date</th>\n        \n        <td>November 29, 2005  21:35:12 GMT</td>\n        \n    </tr>\n    <tr>\n        <th>Experimenter</th>\n        \n        <td>Unknown</td>\n        \n    </tr>\n        <th>Participant</th>\n        \n        <td>Unknown</td>\n        \n    </tr>\n    <tr>\n        <th>Digitized points</th>\n        \n        <td>Not available</td>\n        \n    </tr>\n    <tr>\n        <th>Good channels</th>\n        <td>35 EEG</td>\n    </tr>\n    <tr>\n        <th>Bad channels</th>\n        <td>None</td>\n    </tr>\n    <tr>\n        <th>EOG channels</th>\n        <td>Not available</td>\n    </tr>\n    <tr>\n        <th>ECG channels</th>\n        <td>Not available</td>\n    \n    <tr>\n        <th>Sampling frequency</th>\n        <td>500.00 Hz</td>\n    </tr>\n    \n    \n    <tr>\n        <th>Highpass</th>\n        <td>0.00 Hz</td>\n    </tr>\n    \n    \n    <tr>\n        <th>Lowpass</th>\n        <td>250.00 Hz</td>\n    </tr>\n    \n    \n</table>"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "edf_dir = r\"Jean-Pol_repaired_headers\"\n",
    "name = edfs_names[1]\n",
    "raw = mne.io.read_raw_edf(edf_dir + '//' + name)\n",
    "raw.info"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-05-11T10:39:12.345142Z",
     "end_time": "2023-05-11T10:39:12.494278Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "outputs": [
    {
     "data": {
      "text/plain": "[23]"
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "picked = mne.pick_channels_regexp(raw.info['ch_names'], 'ECG*')\n",
    "picked"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-05-11T10:18:08.411199Z",
     "end_time": "2023-05-11T10:18:08.414195Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "outputs": [
    {
     "data": {
      "text/plain": "pandas._libs.tslibs.timedeltas.Timedelta"
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(stages['Time'][2] - stages['Time'][1])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-05-11T11:03:53.103932Z",
     "end_time": "2023-05-11T11:03:53.121931Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "outputs": [
    {
     "data": {
      "text/plain": "Index([   0,    1,    2,    3,    4,    5,    6,    7,    8,    9,\n       ...\n       1551, 1552, 1553, 1554, 1555, 1556, 1557, 1558, 1559, 1560],\n      dtype='int64', name='Index', length=1561)"
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stages.index -1"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-05-11T11:09:02.000096Z",
     "end_time": "2023-05-11T11:09:02.051315Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "outputs": [],
   "source": [
    "# shift the day if the sleep crosses midnight\n",
    "# will break for leap days and such\n",
    "def fix_midnigt_day(stages):\n",
    "    for i,t in enumerate(stages['Time'][:-1]):\n",
    "        next = stages['Time'][i+1]\n",
    "        if next-t < pd.Timedelta(20,'s'):\n",
    "            print(f'different interval at {next} and {t}')\n",
    "            stages['Time'][i+1:] = stages['Time'][i+1:]+pd.Timedelta(1, 'D')\n",
    "            return stages\n",
    "\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-05-11T11:35:14.875460Z",
     "end_time": "2023-05-11T11:35:14.877451Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "outputs": [
    {
     "data": {
      "text/plain": "                     Time  stage\nIndex                           \n170   1900-01-01 23:59:23      2\n171   1900-01-01 23:59:43      2\n172   1900-01-01 00:00:03      2\n173   1900-01-01 00:00:23      2\n174   1900-01-01 00:00:43      2\n...                   ...    ...\n1556  1900-01-01 07:41:23      0\n1557  1900-01-01 07:41:43      0\n1558  1900-01-01 07:42:03      0\n1559  1900-01-01 07:42:23      0\n1560  1900-01-01 07:42:43      0\n\n[1391 rows x 2 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Time</th>\n      <th>stage</th>\n    </tr>\n    <tr>\n      <th>Index</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>170</th>\n      <td>1900-01-01 23:59:23</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>171</th>\n      <td>1900-01-01 23:59:43</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>172</th>\n      <td>1900-01-01 00:00:03</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>173</th>\n      <td>1900-01-01 00:00:23</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>174</th>\n      <td>1900-01-01 00:00:43</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>1556</th>\n      <td>1900-01-01 07:41:23</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1557</th>\n      <td>1900-01-01 07:41:43</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1558</th>\n      <td>1900-01-01 07:42:03</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1559</th>\n      <td>1900-01-01 07:42:23</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1560</th>\n      <td>1900-01-01 07:42:43</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n<p>1391 rows × 2 columns</p>\n</div>"
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stages[170:]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-05-11T11:35:01.233043Z",
     "end_time": "2023-05-11T11:35:01.241039Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "different interval at 1900-01-01 00:00:03 and 1900-01-01 23:59:43\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_2308/1594200480.py:8: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  stages['Time'][i+1:] = stages['Time'][i+1:]+pd.Timedelta(1, 'D')\n"
     ]
    }
   ],
   "source": [
    "stages = fix_midnigt_day(stages)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-05-11T11:35:17.363227Z",
     "end_time": "2023-05-11T11:35:17.416642Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "outputs": [
    {
     "data": {
      "text/plain": "                     Time  stage\nIndex                           \n170   1900-01-01 23:59:23      2\n171   1900-01-01 23:59:43      2\n172   1900-01-02 00:00:03      2\n173   1900-01-02 00:00:23      2\n174   1900-01-02 00:00:43      2\n...                   ...    ...\n1556  1900-01-02 07:41:23      0\n1557  1900-01-02 07:41:43      0\n1558  1900-01-02 07:42:03      0\n1559  1900-01-02 07:42:23      0\n1560  1900-01-02 07:42:43      0\n\n[1391 rows x 2 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Time</th>\n      <th>stage</th>\n    </tr>\n    <tr>\n      <th>Index</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>170</th>\n      <td>1900-01-01 23:59:23</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>171</th>\n      <td>1900-01-01 23:59:43</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>172</th>\n      <td>1900-01-02 00:00:03</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>173</th>\n      <td>1900-01-02 00:00:23</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>174</th>\n      <td>1900-01-02 00:00:43</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>1556</th>\n      <td>1900-01-02 07:41:23</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1557</th>\n      <td>1900-01-02 07:41:43</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1558</th>\n      <td>1900-01-02 07:42:03</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1559</th>\n      <td>1900-01-02 07:42:23</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1560</th>\n      <td>1900-01-02 07:42:43</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n<p>1391 rows × 2 columns</p>\n</div>"
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stages[170:]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-05-11T11:35:34.883406Z",
     "end_time": "2023-05-11T11:35:34.986454Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
